package com.xxy.mybatispluspro.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xxy.mybatispluspro.mapper.ProjectMapper;
import com.xxy.mybatispluspro.model.entity.Project;
import com.xxy.mybatispluspro.service.ProjectService;
import com.xxy.mybatispluspro.utils.JsonData;
import org.apache.ibatis.executor.ExecutorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/v1/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private QueryWrapper<Project> getQuery(Map<String,Object> map){
        QueryWrapper<Project> queryWrapper =new QueryWrapper<>();
        Object name =map.get("name");
        Object personId = map.get("personId");
        System.out.println(name);
        System.out.println(personId);
        if (name==null && personId == null)
            queryWrapper=null;
        else if(name==null && personId !=null)
            queryWrapper.eq("personId",personId);
        else if(name!=null && personId==null)
            queryWrapper.eq("name",name);
        else
            queryWrapper.eq("personId",personId).eq("name",name);
        return  queryWrapper;

    }

    @GetMapping("list")
    public JsonData allProjects(@RequestParam Map<String,Object> map) {

        QueryWrapper<Project>queryWrapper=getQuery(map);
        if(queryWrapper==null){
            Object redisResults = redisTemplate.opsForValue().get("project");
            if (redisResults !=null){
                System.out.println("从缓存中查找-------------------");
                return JsonData.buildSuccess(redisResults);
            }
            List<Project> list = projectMapper.selectList(queryWrapper);
            redisTemplate.opsForValue().set("project",list);
            return JsonData.buildSuccess(list);
        }
        List<Project> list = projectMapper.selectList(queryWrapper);
        redisTemplate.opsForValue().set("project",list);
        return JsonData.buildSuccess(list);
    }
    @GetMapping("one/{projectId}")
    public JsonData getOne(@PathVariable("projectId") int projectId){
        QueryWrapper<Project>queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",projectId);
        Project project = projectService.getOne(queryWrapper);
        return JsonData.buildSuccess(project);
    }

    @PostMapping("pin/{pinId}")
    public JsonData changePin(@PathVariable("pinId")int pinId,@RequestBody Map<String,Object>map){
        boolean pin = (boolean)map.get("pin");
        UpdateWrapper<Project> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",pinId).set("pin",pin);
        boolean result = projectService.update(updateWrapper);
        System.out.println(result+"-------------------");
        if (result==true){
            redisTemplate.delete("project");
            return JsonData.buildSuccess("更新成功!");
        }
        else {
            return JsonData.buildError("更新失败!");
        }
    }
    @PostMapping("add")
    public JsonData addProject(@RequestBody Map<String,Object>map){
        String name = (String)map.get("name");
        int personId = (int)map.get("personId");
        String organization = (String)map.get("organization");
        Long created = new Date().getTime();
        Project project = new Project();
        project.setName(name);
        project.setOrganization(organization);
        project.setPersonId(personId);
        project.setCreated(created);
        try{
            projectMapper.insert(project);
            redisTemplate.delete("project");
            return JsonData.buildSuccess(project);
        }
        catch (Exception e){
            return JsonData.buildError(e);
        }
    }
    @PostMapping("edit/{projectId}")
    public JsonData editProject(@PathVariable("projectId")int proId,@RequestBody Map<String,Object>map){
        redisTemplate.delete("project");
        String name = (String) map.get("name");
        String organization = (String)map.get("organization");
        int personId = (int)map.get("personId");
        Project project = new Project();
        project.setId(proId);
        project.setName(name);
        project.setOrganization(organization);
        project.setPersonId(personId);
        try {
            projectMapper.updateById(project);
            return JsonData.buildSuccess(project);
        }
        catch (Exception e){
            return JsonData.buildError(e);
        }

    }
}
